package com.choichumgming.rentcat.web.controller;


import com.choichumgming.rentcat.common.api.CommonResult;
import com.choichumgming.rentcat.model.AmsComment;
import com.choichumgming.rentcat.service.AmsCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName AmsCommentController
 * @Description : 管理文章評論功能
 * @Author : choichumgming
 * @Date : 1/3/2020 11:57
*/
@Controller
@Api(tags = "AmsCommentController", description = "文章评论管理")
@RequestMapping("/comment")
public class AmsCommentController {
    @Autowired
    AmsCommentService amsCommentService;

    @ApiOperation("添加文章评论")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@RequestBody AmsComment comment) {
        int count = amsCommentService.add(comment);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除文章评论")
    @RequestMapping(value = "/delete/{commentId}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long commentId) {
        int count = amsCommentService.delete(commentId);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
    @ApiOperation("获得文章所有评论")
    @RequestMapping(value = "/item/{articleId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<AmsComment>> getList(@PathVariable("articleId") Long articleId) {
        List<AmsComment> list = amsCommentService.getArticleComments(articleId);
        if (list.size() > 0) {
            return CommonResult.success(list);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取评论详情")
    @RequestMapping(value = "/{commentId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<AmsComment> item(@PathVariable Long commentId) {
        AmsComment comment = amsCommentService.getItem(commentId);
        return CommonResult.success(comment);
    }


    @ApiOperation("显示指定用户所有评论")
    @RequestMapping(value = "/list/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<AmsComment>> list(@PathVariable Long userId) {
        List<AmsComment> articleList = amsCommentService.getUserCommentlist(userId);
        return CommonResult.success(articleList);
    }
}
